import { baseUrl } from './api.js'

function upload(){
	const token = uni.getStorageSync('token')
	return new Promise((resolve,reject)=>{
		uni.chooseImage({
			count: 1, 
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album'], //从相册选择
			success: function (res) {
				let tempFilePaths = res.tempFilePaths;
				let uploadTask = uni.uploadFile({
				    url: baseUrl+'/common/upload',
				    filePath: tempFilePaths[0],
				    name: 'file',
					formData: {
						token: token,
					},
				    success(cts) {
						let data = JSON.parse(cts.data)
						if(data.code == '1'){
							resolve(data.data.url)
						}else{
							uni.showToast({
								icon:"none",
								title:'上传失败'
							})
							reject(data.msg)
						}
				    },
					fail(err) {
						console.log(err)
						// 一般是网络错误
						uni.showModal({
							content:'网络错误请重试',
							showCancel:false
						})
						reject(new Error('网络错误请重试'))
					}
				});
			},
			fail(err) {
				console.log(err)
				// 一般是网络错误
				uni.showToast({
					icon:"none",
					title:"已取消"
				})
				reject(new Error('已取消'))
			}
		});
	})
}

function uploads(){
	const token = uni.getStorageSync('token')
	let imgs = []
	uni.showLoading({
		title: '正在上传...',
		mask: true
	});
	return new Promise((resolve,reject)=>{
		uni.chooseImage({
			count: 9, 
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album'], //从相册选择
			success: function (res) {
				let tempFilePaths = res.tempFilePaths;
				for(let i=0;i<tempFilePaths.length;i++){
					let uploadTask = uni.uploadFile({
					    url: baseUrl+'/common/upload',
					    filePath: tempFilePaths[i],
					    name: 'file',
						formData: {
							token: token,
						},
					    success(cts) {
							let data = JSON.parse(cts.data)
							if(data.code == '1'){
								imgs.push(data.data.url)
								if(i==tempFilePaths.length-1){
									resolve(imgs)
								}
							}else{
								uni.showToast({
									icon:"none",
									title:'上传失败'
								})
								reject(data.msg)
							}
					    },
						fail(err) {
							console.log(err)
							// 一般是网络错误
							uni.showModal({
								content:'网络错误请重试',
								showCancel:false
							})
							reject(new Error('网络错误请重试'))
						}
					});
				}
			},
			fail(err) {
				console.log(err)
				// 一般是网络错误
				uni.showToast({
					icon:"none",
					title:"已取消"
				})
				reject(new Error('已取消'))
			},
			complete() {
				// 请求结束后关闭Loading
				uni.hideLoading()
			},
		});
	})
}

//上传视频
function uploadVideo(){
	const token = uni.getStorageSync('token')
	return new Promise((resolve,reject)=>{
		uni.chooseVideo({
			count: 1, 
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', ['camera', 'album']], //从相册选择
			success: function (res) {
				let tempFilePath = res.tempFilePath;
				let uploadTask = uni.uploadFile({
				    url: baseUrl+'/common/upload',
				    filePath: tempFilePath,
				    name: 'file',
					formData: {
						token: token,
					},
				    success(cts) {
						let data = JSON.parse(cts.data)
						if(data.code == '1'){
							resolve(data.data.url)
						}else{
							uni.showToast({
								icon:"none",
								title:'上传失败'
							})
							reject(data.msg)
						}
				    },
					fail(err) {
						console.log(err)
						// 一般是网络错误
						uni.showModal({
							content:'网络错误请重试',
							showCancel:false
						})
						reject(new Error('网络错误请重试'))
					}
				});
			},
			fail(err) {
				console.log(err)
				// 一般是网络错误
				uni.showToast({
					icon:"none",
					title:"已取消"
				})
				reject(new Error('已取消'))
			}
		});
	})
}

//上传文件
function uploadFile(){
	const token = uni.getStorageSync('token')
	return new Promise((resolve,reject)=>{
		uni.chooseMessageFile({
			count: 1, 
			type: 'file',
			extension: ['pdf','ppt','zip'],
			success: function (res) {
				let tempFilePaths = res.tempFiles;
				let uploadTask = uni.uploadFile({
				    url: baseUrl+'/common/upload',
				    filePath: tempFilePaths[0]['path'],
				    name: 'file',
					formData: {
						token: token,
					},
				    success(cts) {
						let data = JSON.parse(cts.data)
						if(data.code == '1'){
							resolve(data.data.url)
						}else{
							uni.showToast({
								icon:"none",
								title:'上传失败'
							})
							reject(data.msg)
						}
				    },
					fail(err) {
						console.log(err)
						// 一般是网络错误
						uni.showModal({
							content:'网络错误请重试',
							showCancel:false
						})
						reject(new Error('网络错误请重试'))
					}
				});
			},
			fail(err) {
				console.log(err)
				// 一般是网络错误
				uni.showToast({
					icon:"none",
					title:"已取消"
				})
				reject(new Error('已取消'))
			}
		});
	})
}

 
module.exports = {
    upload : upload,
	uploads : uploads,
	uploadVideo: uploadVideo,
	uploadFile: uploadFile
}

